<?php

/**
 * This is the model class for table "zy_equipment".
 *
 * The followings are the available columns in table 'zy_equipment':
 * @property integer $equipment_id
 * @property string $equipment_name
 * @property string $equipment_description
 * @property integer $service_id
 * @property string $equipment_number
 * @property string $MacAddress
 * @property string $telephone_number
 */
class Equipment extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Equipment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_equipment';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('equipment_name, equipment_description,service_id', 'required'),
			array('service_id,creater,updater,istop', 'numerical', 'integerOnly'=>true),
		    array('MacAddress','unique'),
			array('equipment_name, equipment_description,equipment_number, MacAddress, telephone_number,CardNumber,city,server,remark,create_time,update_time', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('equipment_id, equipment_name, equipment_description,service_id, equipment_number, MacAddress, telephone_number,CardNumber,city,server,remark,create_time,update_time,creater,updater', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'custom'=>array(self::BELONGS_TO,'Custom','equipment_id'),
				'userservice'=>array(self::HAS_MANY,'UserService','equipment_id')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'equipment_id' => Yii::t('base','Equipment'),
			'equipment_name' => Yii::t('base','Equipment Name'),
			'equipment_description' => Yii::t('base','Equipment Description'),
			'service_id' => Yii::t('base','Service Name'),
			'equipment_number'=>Yii::t('base','Equipment Number'),
			'MacAddress'=>Yii::t('base','MacAddress'),
			'telephone_number'=>Yii::t('base','Telephone Number'),
			'CardNumber'=>Yii::t('base','Card Number'),
			'city'=>Yii::t('base','City'),
			'server'=>Yii::t('base','Server'),
			'remark'=>Yii::t('base','Remark'),
			'create_time'=>Yii::t('base','Create Time'),
		    'update_time'=>Yii::t('base','Update Time'),
		    'creater'=>Yii::t('base','Creater'),
			'updater'=>Yii::t('base','Equipment Owner'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('equipment_id',$this->equipment_id);
		$criteria->compare('equipment_name',$this->equipment_name,true);
		$criteria->compare('equipment_description',$this->equipment_description,true);
		$criteria->compare('service_id',$this->service_id);
		$criteria->compare('equipment_number',$this->equipment_number,true);
		$criteria->compare('MacAddress',$this->MacAddress,true);
		$criteria->compare('telephone_number',$this->telephone_number,true);
		$criteria->compare('CardNumber',$this->CardNumber,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('server',$this->server,true);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('creater',$this->creater,true);
		$criteria->compare('updater',$this->updater,true);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function search2($results)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
	
		$criteria=new CDbCriteria;
	
		$criteria->compare('equipment_id',$this->equipment_id);
		$criteria->addInCondition('equipment_id',$results);
		$criteria->compare('equipment_name',$this->equipment_name,true);
		$criteria->compare('equipment_description',$this->equipment_description,true);
		$criteria->compare('service_id',$this->service_id);
		$criteria->compare('equipment_number',$this->equipment_number,true);
		$criteria->compare('MacAddress',$this->MacAddress,true);
		$criteria->compare('telephone_number',$this->telephone_number,true);
		$criteria->compare('CardNumber',$this->CardNumber,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('server',$this->server,true);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('creater',$this->creater,true);
		$criteria->compare('updater',$this->updater,true);
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}
	public function showServiceName($id)
	
	{
	
		$rs = Equipment::model()->findByPk($id);
	
		$rs2 = Service::model()->find('service_id='.$rs->service_id);
	
		return $rs2->service_name;
	
	}
	public function getEquipmentNumber()
	{
		$sql="select equipment_number from zy_equipment where equipment_name = 'wifi' and isused = 0";

		$connection = Yii::app()->db;
		
		$command = $connection->createCommand($sql);
		
		$results = $command->queryAll();
		
		for($i=0;$i<count($results);$i++){
		
			$result[$i]=$results[$i]['equipment_number'];
		
		}
		
		return $result;
	}
	public function createUserName()
	{
		$user_name=self::setuser();
		$password=rand(10000000, 99999999);
		$user=new User;
		$user->user_name=$user_name;
		$user->password=$password;
		$user->level=8;
		$user->save();
		return $user->user_id;
	}
	public  function setuser()
	{
		$user_name=rand(10000000, 99999999);
		$user_result=User::model()->find('user_name='.$user_name);
		if($user_result!=""){
			setuser();
		}
		return $user_name;
	}
	public function updateEquipment($equipment_id)
	{
		$equipment=Equipment::model()->find('equipment_id='.$equipment_id);
		$equipment->updater=yii::app()->user->id;
		$equipment->isused=2;
		return $equipment->save();
	}
	public function updateUserService($user_id)
	{
		$service = Service::model()->find("code="."'phone'");
		$result2=new UserService;
		$result2->user_id=$user_id;
		$result2->service_id=$service->service_id;
		$result2->save();
	}
	public function updateLoginList($package_id,$user_id)
	{
		if($package_id==6){
			$package_details=PackageDetails::model()->find("package_type=:package_type AND isused =:isused",array(":package_type"=>'20',":isused"=>0));
		}elseif($package_id==7){
			$package_details=PackageDetails::model()->find('package_type=:package_type AND isused=:isused',array(":package_type"=>'50',":isused"=>0));
		}elseif($package_id==8){
			$package_details=PackageDetails::model()->find('package_type=:package_type AND isused=:isused',array(":package_type"=>'100',":isused"=>0));
		}elseif($package_id==9){
			$package_details=PackageDetails::model()->find('package_type=:package_type AND isused=:isused',array(":package_type"=>'10',":isused"=>0));
		}
		$package_details_id = $package_details->package_details_id;
		$field1=$package_details->name;
		$field2=$package_details->password;
		$field3="billing3.novatel.com.au";
		if($field1!=""){
			$service = Service::model()->find("code="."'phone'");
			$userservice=UserService::model()->find('user_id=:user_id AND service_id=:service_id',array(':user_id'=>$user_id,':service_id'=>$service->service_id));
			$user_service_id=$userservice->user_service_id;
			$loginlist_result=new LoginList;
			$loginlist_result->user_service_id=$user_service_id;
			$loginlist_result->field1=$field1;
			$loginlist_result->field2=$field2;
			$loginlist_result->field3=$field3;
			$loginlist_result->save();
			$package_details_result=PackageDetails::model()->find('package_details_id='.$package_details_id);
			$package_details_result->isused=1;
			return $package_details_result->save();
		}
	}
	public function createcustom($user_id,$equipment_id,$equipment_number,$package_id)
	{
		$custom=new Custom;
		$custom->user_id=$user_id;
		$custom->equipment_id=$equipment_id;
		$custom->equipment_number=$equipment_number;
		$custom->package_id=$package_id;
		$custom->create_time=date('Y-m-d H:i:s',time());
		return $custom->save();
	}
	//查询客户押金
	public function getDeposit($equipment_number){
		$custom=Custom::model()->find("equipment_number="."'$equipment_number'");
		return $custom->deposit;
	}
	public function getIsreback($equipment_number){
		$userservice=UserService::model()->find("equipment_number="."'$equipment_number'");
		if($userservice->user_id==0){
			return Yii::t('base','Undistribute');
		}else{
			if($userservice->isreback==0){
				return Yii::t('base','Saled');
			}elseif($userservice->isreback==1){
				return Yii::t('base','Historical Custom');
			}else{
				return Yii::t('base','Unsale');
			}
		}
	}
	public function getcustomId($id){
		$user=User::model()->findByPk($id);
		if($user->user_id>0)
			$custom=Custom::model()->find('user_id='.$user->user_id);
		else
			$custom=Custom::model();
		return $custom->custom_id;
	}
	public function judgeA($equipment_number){
		$userservice=UserService::model()->find("equipment_number="."'$equipment_number'");
		if($userservice->user_id==0){
			return true;
		}else{
			return false;
		}
	}
	public function judgeB($equipment_number){
		$userservice=UserService::model()->find("equipment_number="."'$equipment_number'");
		if($userservice->isreback==2){
			return true;
		}else{
			return false;
		}
	}
	public function judgeC($equipment_number){
		$userservice=UserService::model()->find("equipment_number="."'$equipment_number'");
		if($userservice->isreback==1 && $userservice->user_id!=0){
			return true;
		}else{
			return false;
		}
	}
	//代理人所有的设备
	public function AgentEquipment(){
		$id=yii::app()->user->id;
		if(yii::app()->user->level==5){
			$sql = " SELECT * FROM zy_equipment WHERE updater=".$id." AND is_used = 1 AND istop = 1 AND service_id =12";
		}else{
			$sql = " SELECT * FROM zy_equipment WHERE updater=".$id." AND is_used = 1 AND istop = 0 AND service_id =12";
		}
		$rs=Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::listData($rs,'equipment_number','equipment_number');
	}
}